/**
 * 
 */
package com.wubo.sec.web.action;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.wubo.common.util.StringUtils;
import com.wubo.sec.SecurityManager;
import com.wubo.sec.filter.GroupBeforeFilter;
import com.wubo.sec.model.Group;
import com.wubo.sec.service.IGroupService;

import flexjson.JSONSerializer;

/**
 * @author wubo
 * @CreateDate 2010-8-6
 * @version 1.0.01
 */
@Controller
public class GroupMgtAction extends BaseSecAction {

	@Autowired
	private IGroupService groupService;
	
	private List<Group> groups;
	
	private Group group;
	
	private String id;
	
	private String groupName;
	
	private String orderby;
	
	private String markLocked;
	
	private String mark;

	public String getMark() {
		return mark;
	}

	public void setMark(String mark) {
		this.mark = mark;
	}

	public String getMarkLocked() {
		return markLocked;
	}

	public void setMarkLocked(String markLocked) {
		this.markLocked = markLocked;
	}

	public IGroupService getGroupService() {
		return groupService;
	}

	public void setGroupService(IGroupService groupService) {
		this.groupService = groupService;
	}

	public List<Group> getGroups() {
		return groups;
	}

	public void setGroups(List<Group> groups) {
		this.groups = groups;
	}

	public Group getGroup() {
		return group;
	}

	public void setGroup(Group group) {
		this.group = group;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}
	
	public String getGroupName() {
		return groupName;
	}

	public void setGroupName(String groupName) {
		this.groupName = groupName;
	}

	public String getOrderby() {
		return orderby;
	}

	public void setOrderby(String orderby) {
		this.orderby = orderby;
	}

	public String list(){
		String where = "where 1=1";
		if(StringUtils.hasText(groupName)){
			where = " where g.name like '%"+groupName+"%'";
		}
		if(StringUtils.hasText(mark)){
			where += " and mark like '%"+mark+"%'";
		}
		if(!StringUtils.isEmpty(markLocked)){
			if(!markLocked.trim().equals("2")){
				where += " and markLocked="+markLocked;
			}
		}
		if(StringUtils.isEmpty(orderby)){
			orderby = " order by name";
		}
		
		where += new GroupBeforeFilter().sift();
		
		total = groupService.getTotal(where);
		
		where += orderby;
		
		groups = groupService.getSplitPage(start, limit, where);
		
		setUserAttributesToRequest();
		
		return SUCCESS;
	}
	
	public String listForAssign(){
		String where = "where 1=1"+new GroupBeforeFilter().sift();
		
		groups = groupService.findRunAs(where, null);
		
		return "radio";
	}

	private String q; //用于AutoComplete
	public String getQ() {
		return q;
	}
	public void setQ(String q) {
		this.q = q;
	}
	public void autoComplete() {
		try{
			List<String[]> names = groupService.listName(q);
			json(new JSONSerializer().serialize(names));
		}catch(Throwable t){
			logger.error(t.getMessage(), t);
			jsonError(null);
		}
	}
	
	public void autoCompleteMark() {
		try{
			List<String[]> names = groupService.listMark(q);
			json(new JSONSerializer().serialize(names));
		}catch(Throwable t){
			logger.error(t.getMessage(), t);
			jsonError(null);
		}
	}
	
	public String add(){
		try{
			if(! StringUtils.hasText(group.getMark()) ){
				if(SecurityManager.isAdmin()){
					group.setMark(Group.DEFAULT_MARK);
				}else{
					group.setMark(SecurityManager.getCurrentUser().getGroup().getMark());
				}
				group.setMarkLocked(true);
			}
			int priority = SecurityManager.getPriorityOfCurrentUser();
			if( group.getPriority() > priority){
				group.setPriority(priority);
			}
			groupService.create(group);

			jsonSuccess(null);
			
		}catch(RuntimeException t){
			jsonError(null);
			throw t;
		}
		return null;
	}
	
	public String edit(){
		try{
			groupService.update(group);
	
			jsonSuccess(null);
			
		}catch(RuntimeException t){
			jsonError(null);
			throw t;
		}
		return null;
	}
	
	public String del(){
		groupService.delete(groupService.get(id));
		flag = true;
		
		list();
		
		return SUCCESS;
	}
}
